package com.example.football;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.football.entity.Schedule;
import com.example.football.entity.Yuyue;
import com.example.football.util.JDBCUtils;
import com.example.football.util.SetTeamlogo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class SiteshowActivity extends AppCompatActivity {

    ListView myList;
    static public Integer peopleid ;
    static public String account;
    static public String name;
    static public ArrayList<Yuyue> list;
    static public String site;
    static public String byteam;

    SimpleDateFormat formatter;
    Date curDate;
    String   currentTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_siteshow);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }

        Intent intent=getIntent();
        site = intent.getStringExtra("site");

        peopleid = Main2Activity.peopleid;
        account = Main2Activity.account;
        name = Main2Activity.name;
        byteam = Main2Activity.byteam;
        list= new ArrayList<>();

        formatter   =   new   SimpleDateFormat   ("yyyy-MM-dd");
        curDate =  new Date(System.currentTimeMillis());
        currentTime = formatter.format(curDate);

        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("site",site);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String url="http://"+ JDBCUtils.ip +":9000/APPfindyuyuebysite";
        RequestQueue requestQueue= Volley.newRequestQueue(SiteshowActivity.this);
        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, url,jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {

                try {
                    JSONArray detail = jsonObject.getJSONArray("detail");
                    for(int i = 0; i < detail.length(); i++){
                        JSONObject data = detail.getJSONObject(i);
                        if(currentTime.compareTo(data.getString("date"))<=0){
                            list.add(new Yuyue(data.getInt("id"),data.getString("team1"),data.getString("team2"),
                                    data.getString("date"),data.getString("time"),data.getString("site")));
                        }

                    }
                    myList = findViewById(R.id.mylist6);
                    final MyAdapter myAdapter = new MyAdapter();
                    myList.setAdapter(myAdapter);
                    myAdapter.setOnItemDeleteClickListener(new onItemDeleteListener() {
                        @Override
                        public void onDeleteClick(int i) {
                            if(list.get(i).getTeam1().equals("加入")){
                                updateYuyueTeam1(list.get(i).getId());
                            }else {
                                Toast.makeText(SiteshowActivity.this, "已被预约，请选择其他时间预约！", Toast.LENGTH_SHORT).show();
                            }
                            myAdapter.notifyDataSetChanged();
                        }
                    });

                    myAdapter.setOnItemDeleteClickListener1(new onItemDeleteListener1() {
                        @Override
                        public void onDeleteClick1(int i) {
                            if(list.get(i).getTeam2().equals("加入")){
                                updateYuyueTeam2(list.get(i).getId());
                            }else {
                                Toast.makeText(SiteshowActivity.this, "已被预约，请选择其他时间预约！", Toast.LENGTH_SHORT).show();
                            }
                            myAdapter.notifyDataSetChanged();
                        }
                    });
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(SiteshowActivity.this, "网络出错", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }


    public class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {//获取item的数量
            return list.size();
        }

        @Override
        public Object getItem(int i) {//i表示当前item的序号，序号从0开始编写，getIte用于获取某个item
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {//获取选中的item的编号
            return i;
        }

        @Override//用于配置listview要加载的内容，包括视图和数据
        public View getView(final int i, View view, ViewGroup viewGroup) {

//             优化写法
            ViewHolder holder = null;
            if(view == null){
                //这里是将item视图与view绑定起来
                view = View.inflate(SiteshowActivity.this,R.layout.siteshow_item,null);
                holder = new ViewHolder();
//                注意，这里是去view的视图里面拿id
                holder.team1 = view.findViewById(R.id.team1);
                holder.team2 = view.findViewById(R.id.team2);
                holder.time = view.findViewById(R.id.datetime);
                holder.site = view.findViewById(R.id.site);
                holder.date = view.findViewById(R.id.date);

                holder.team1.setText(list.get(i).getTeam1());
                holder.team2.setText(list.get(i).getTeam2());
                holder.time.setText(list.get(i).getTime());
                holder.site.setText(list.get(i).getSite());
                holder.date.setText(list.get(i).getDate());
                view.setTag(holder);
            }else {
                holder = (ViewHolder) view.getTag();
                holder.team1.setText(list.get(i).getTeam1());
                holder.team2.setText(list.get(i).getTeam2());
                holder.time.setText(list.get(i).getTime());
                holder.site.setText(list.get(i).getSite());
                holder.date.setText(list.get(i).getDate());
            }

            holder.team1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view1) {
                    mOnItemDeleteListener.onDeleteClick(i);
                }
            });
            holder.team2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view1) {
                    mOnItemDeleteListener1.onDeleteClick1(i);
                }
            });
            return view;
        }

        private onItemDeleteListener mOnItemDeleteListener;

        private onItemDeleteListener1 mOnItemDeleteListener1;

        public void setOnItemDeleteClickListener(onItemDeleteListener mOnItemDeleteListener) {
            this.mOnItemDeleteListener = mOnItemDeleteListener;
        }

        public void setOnItemDeleteClickListener1(onItemDeleteListener1 mOnItemDeleteListener1) {
            this.mOnItemDeleteListener1 = mOnItemDeleteListener1;
        }
    }

    public interface onItemDeleteListener {
        void onDeleteClick(int i);
    }

    public interface onItemDeleteListener1 {
        void onDeleteClick1(int i);
    }

    static class ViewHolder{
        TextView time;
        TextView date;
        TextView team1;
        TextView team2;
        TextView site;
    }

    public void updateYuyueTeam1(int id){
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("id",id);
            jsonObject.put("team1",byteam);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String url="http://"+ JDBCUtils.ip +":9000/APPupdateyuyuebyteam1";
        RequestQueue requestQueue= Volley.newRequestQueue(SiteshowActivity.this);
        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, url,jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                    Toast.makeText(SiteshowActivity.this, "预约成功", Toast.LENGTH_SHORT).show();
                    finish();
                    Intent intent=new Intent(SiteshowActivity.this,SiteshowActivity.class);
                    intent.putExtra("site",site);
                    startActivity(intent);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(SiteshowActivity.this, "网络出错", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }

    public void updateYuyueTeam2(int id){
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("id",id);
            jsonObject.put("team2",byteam);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String url="http://"+ JDBCUtils.ip +":9000/APPupdateyuyuebyteam2";
        RequestQueue requestQueue= Volley.newRequestQueue(SiteshowActivity.this);
        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, url,jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                    Toast.makeText(SiteshowActivity.this, "预约成功", Toast.LENGTH_SHORT).show();
                    finish();
                    Intent intent=new Intent(SiteshowActivity.this,SiteshowActivity.class);
                    intent.putExtra("site",site);
                    startActivity(intent);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(SiteshowActivity.this, "网络出错", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.siteshow_iv_back:
                finish();
                break;

        }
    }
}
